//
//  HZJSingerMusicViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/7.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZJSingerMusicViewController.h"
#import "HZJMusicManagerViewController.h"

#import "HSAFNetTool.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"

#import "HZJSmallTableViewCell.h"
#import "HZJListTableViewCell.h"
#import "HZJSongDetailModel.h"

@interface HZJSingerMusicViewController ()<UITableViewDelegate, UITableViewDataSource, musicManager>

@property (nonatomic, strong) UIImageView *HeadImage;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *nameLable;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

#define BackGroupHeight  self.view.bounds.size.height / 3
#define URL @"&size=50&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.88265664085061&longtitude=121.5391135272595"

@implementation HZJSingerMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
    
    [self createTabView];
    [self imageLaShen];
    [self HeadImage];
    
    self.titleLable = [[UILabel alloc] init];
    _titleLable.frame = CGRectMake(0, 0, 150, 20);
    self.titleLable.text = _detailStr;
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.textColor = [UIColor redColor];
    self.navigationItem.titleView = _titleLable;
    
#pragma mark - 调用数据请求
    [self getData];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Jback"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
}

- (void)leftAction {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
#pragma mark - 上方图片拉伸
- (void)imageLaShen {
    
    self.HeadImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -BackGroupHeight, self.view.frame.size.width, BackGroupHeight)];
    [_HeadImage sd_setImageWithURL:[NSURL URLWithString:_picUrl]];
    
    [self.tableView addSubview:_HeadImage];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = scrollView.contentOffset.y;
    if (yOffset < -BackGroupHeight) {
        CGRect rect = _HeadImage.frame;
        rect.origin.y = yOffset;
        rect.size.height = -yOffset;
        _HeadImage.frame = rect;
        
    }
    
    CGFloat alpha = (yOffset + BackGroupHeight) / BackGroupHeight;
    
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor lightGrayColor]colorWithAlphaComponent:alpha]]forBarMetrics:UIBarMetricsDefault];
    _titleLable.alpha = alpha;
    self.navigationController.navigationBarHidden = NO;
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    //    开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    //    获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //    使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    //    渲染上下文
    CGContextFillRect(context, rect);
    //    从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    //    结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
    
}
#pragma mark - 创建tableView
- (void)createTabView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentInset = UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [_tableView registerClass:[HZJSmallTableViewCell class] forCellReuseIdentifier:@"HZJSmallTableViewCell"];
    [_tableView registerClass:[HZJListTableViewCell class] forCellReuseIdentifier:@"HZJListTableViewCell"];
    [self.view addSubview:_tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 70;
    }
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        HZJSmallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZJSmallTableViewCell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.smountLable.text = [NSString stringWithFormat:@"%ld首", self.dataSource.count];
        cell.delegate = self;
        return cell;
    } else {
        
        HZJListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZJListTableViewCell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.songModel = self.dataSource[indexPath.row - 1];
        cell.numLable.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        return cell;
    }
}

#pragma mark - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        NSNotificationCenter *hsdataSource = [NSNotificationCenter defaultCenter];
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        
        [array addObject:_dataSource];
        
        NSString *str = [NSString stringWithFormat:@"%d", 1];
        [array addObject:str];
        
        [hsdataSource postNotificationName:@"hsdataSource" object:array userInfo:nil];
        
        
    } else {
        
        NSNotificationCenter *hsdataSource = [NSNotificationCenter defaultCenter];
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        
        [array addObject:_dataSource];
        
        NSString *str = [NSString stringWithFormat:@"%ld", indexPath.row];
        [array addObject:str];
        
        [hsdataSource postNotificationName:@"hsdataSource" object:array userInfo:nil];
        
    }
    
    

    
    
    
    
}

#pragma mark - 数据请求
- (void)getData {
    
    [HSAFNetTool GETNetWithUrl:[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1%@", _urlString, URL] body:nil headerFile:nil response:HSJSON success:^(id result) {
        self.dataSource = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"]) {
            HZJSongDetailModel *songListModel = [[HZJSongDetailModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:songListModel];
        }
        NSLog(@"%@", self.dataSource);
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
#pragma mark - 下载界面
- (void)MusicDownLoad {
    
   
    HZJMusicManagerViewController *musicVC = [[HZJMusicManagerViewController alloc] init];
    musicVC.data = self.dataSource;
    [self.navigationController pushViewController:musicVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
